package com.swufe.team;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class MapTask implements Runnable {
    private static final String TAG="MapTask";
    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        //获取网络数据并返回
        Log.i(TAG, "run: Running....");
        try {
            Thread.sleep(3000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }


        ArrayList<HashMap<String,String>> reList=new ArrayList<HashMap<String,String>>();

        try {
            Document doc = Jsoup.connect(" https://www.boc.cn/sourcedb/whpj/  ").get();

            Elements tables= doc.getElementsByTag("table");
            Element table=tables.get(1);
            Elements tds=table.getElementsByTag("td");
            Log.i(TAG, "run: tds"+tds);
            if(tds.size()>0) {
                for (int i = 8; i < tds.size(); i += 8) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    Element td1 = tds.get(i);
                    Element td2 = tds.get(i + 5);
                    String std1 = td1.text();
                    String std2 = td2.text();
                    map.put("ItemTitle", std1);//标题
                    map.put("ItemDetail", std2);//
                    reList.add(map);
                    Log.i(TAG, "run: " + std1 + "==>" + std2);

                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        Message msg=handler.obtainMessage(7,reList);
        handler.sendMessage(msg);
        Log.i(TAG, "run: sendMessage ok");
    }
}
